# 读取文件
def read_file(path: str):
    # 定义一个集合接收读取的数据
    students = []
    # 开始读取
    try:
        # 使用with打开文件
        with open(path, mode='r', encoding='UTF-8') as fd:
            # 一次性读取整个文件
            # content = fd.read()
            # print(content)
            # 逐行读取
            one_line = fd.readline()
            print(one_line)
            while one_line:
                one_line_list = one_line.strip().split(",")
                students.append(one_line_list)
                one_line = fd.readline()
    except Exception as e:
        print(str(e))
    return students


def read_file_dict(path: str, infos: list):
    students = []
    try:
        with open(path, mode='r', encoding='UTF-8') as fd:
            one_line = fd.readline()
            while one_line:
                one_line_list = one_line.strip().split(',')
                one_student = {}
                for index, val in enumerate(one_line_list):
                    one_student[infos[index]] = val
                students.append(one_student)
                one_line = fd.readline()
    except Exception as e:
        print("文件读取错误:", e)
    return students


if __name__ == '__main__':
    # r表示路径中的斜杠取消转义
    path = r"E:\python\project\Pj01\readfile\students.csv"
    # file = read_file(path)

    dict_list = read_file_dict(path, infos=['name', 'gender', 'age', 'class'])
    print(dict_list)
